package cc.rengu.igas.bomp.core.service.trans;


import cc.rengu.igas.bomp.common.dao.TxnLimitCfgMapper;
import cc.rengu.igas.bomp.common.dao.impl.TxnLimitCfgMapperImpl;
import cc.rengu.igas.bomp.common.entity.TxnLimitCfg;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.TxnLimitCfgBean;
import cc.rengu.igas.bomp.facade.request.ChannelLimitCfgQueryRequest;
import cc.rengu.igas.bomp.facade.response.ChannelLimitCfgQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.LimitTypeEnum;
import cc.rengu.oltp.service.common.enums.TransStatusEnum;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户限额配置查询
 * Created by wangcheng
 */
public class ChannelLimitCfgQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        ChannelLimitCfgQueryRequest channelLimitCfgQueryRequest = new ChannelLimitCfgQueryRequest();
        ConvertUtil.convertOutput(channelLimitCfgQueryRequest);
        return channelLimitCfgQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        ChannelLimitCfgQueryRequest channelLimitCfgQueryRequest = (ChannelLimitCfgQueryRequest) request;
        ChannelLimitCfgQueryResponse channelLimitCfgQueryResponse = new ChannelLimitCfgQueryResponse();
        BizResponse<ChannelLimitCfgQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.IGAS);
        BeanUtil.beanCopy(channelLimitCfgQueryRequest, channelLimitCfgQueryResponse);

        String instId = channelLimitCfgQueryRequest.getHeader().getInstId();
        String channelId = channelLimitCfgQueryRequest.getChannelId();
        String channelTxn = channelLimitCfgQueryRequest.getChannelTxn();
        String limitAcctType = channelLimitCfgQueryRequest.getLimitAcctType();

        String limitType;
        String limitInst;
        List<TxnLimitCfg> limitCfgs;
        TxnLimitCfgMapper txnLimitCfgMapper = new TxnLimitCfgMapperImpl();
        List<TxnLimitCfgBean> channelLimitList = new ArrayList<>();        /* 通道限额配置 */
        if (!StringUtil.isEmptyOrNull(channelId)) {
            limitType = LimitTypeEnum.CHANNEL.getLimitType();
            limitInst = channelId;
            limitCfgs = txnLimitCfgMapper.selectTxnLimitCfgLimitTypeInst(instId, limitType, limitInst);
            if (null != limitCfgs && !limitCfgs.isEmpty()) {
                convTxnLimitCfg(limitCfgs, channelLimitList);
            }
        }
        if (!StringUtil.isEmptyOrNull(limitAcctType)) {
            channelLimitList = channelLimitList.stream().filter(item -> item.getLimitAcctType().equals(limitAcctType)|| "*".equals(item.getLimitAcctType())).collect(Collectors.toList());
        }

        List<TxnLimitCfgBean> channelTxnLimitList = new ArrayList<>();        /* 收银台限额配置 */
        if (!StringUtil.isEmptyOrNull(channelId) && !StringUtil.isEmptyOrNull(channelTxn)) {
            limitType = LimitTypeEnum.CHANNEL_TRANS.getLimitType();
            limitInst = channelId + "|" + channelTxn;
            limitCfgs = txnLimitCfgMapper.selectTxnLimitCfgLimitTypeInst(instId, limitType, limitInst);
            if (null != limitCfgs && !limitCfgs.isEmpty()) {
                convTxnLimitCfg(limitCfgs, channelTxnLimitList);
            }
        }
        if (!StringUtil.isEmptyOrNull(limitAcctType)) {
            channelTxnLimitList = channelTxnLimitList.stream().filter(item -> item.getLimitAcctType().equals(limitAcctType)|| "*".equals(item.getLimitAcctType())).collect(Collectors.toList());
        }
        channelLimitCfgQueryResponse.setChannelLimitList(channelLimitList);
        channelLimitCfgQueryResponse.setChannelTxnLimitList(channelTxnLimitList);
        bizResponse.setResult(channelLimitCfgQueryResponse);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setTransStatus(TransStatusEnum.TRANS_SUCC);
        bizResponse.setRespDesc("限额查询成功");
        bizResponse.setRspSysId(ChannelEnum.BOMP.getChannelType());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        ChannelLimitCfgQueryResponse channelLimitCfgQueryResponse = (ChannelLimitCfgQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(channelLimitCfgQueryResponse);
    }


    private void convTxnLimitCfg(List<TxnLimitCfg> limitCfgs, List<TxnLimitCfgBean> transLimitList) {
        for (TxnLimitCfg limitCfg : limitCfgs) {
            TxnLimitCfgBean bean = new TxnLimitCfgBean();
            BeanUtil.beanCopy(limitCfg, bean);
            transLimitList.add(bean);
        }
    }
}
